package repository

import (
	"admin-system-go/model/do"
	"encoding/json"
	"fmt"
	"github.com/mitchellh/mapstructure"
	"reflect"
)

func init() {
	var count int64
	GetDb().Model(&do.User{}).Count(&count)
	if count > 0 {
		fmt.Println("database had init")
		return
	}
	InitBaseSystemResource(&do.User{}, baseUser)
	InitBaseSystemResource(&do.Menu{}, baseMenu)
	InitBaseSystemResource(&do.Permission{}, basePermission)
	InitBaseSystemResource(&do.UserGroup{}, baseUserGroup)
	InitBaseSystemResource(&do.Role{}, baseRole)
	InitBaseSystemResource(&do.Resource{}, baseResource)
	InitBaseSystemResource(&do.ResourceGroup{}, baseResourceGroup)
	InitRelationSystemResource()
}

func InitBaseSystemResource(target interface{}, data string) {
	if target == nil {
		fmt.Println("参数不能未空")
		return
	}
	t := reflect.TypeOf(target)
	if t.Kind() == reflect.Ptr {
		t = t.Elem()
	} else {
		panic("参数错误！必须传入指针类型")
	}
	if t.PkgPath() != "admin-system-go/model/do" {
		fmt.Println("参数必须为admin-system-go/model/do到结构体")
		return
	}
	var total int64

	GetDb().Model(target).Count(&total)
	if total > 0 {
		fmt.Println("skip init " + t.Name())
		return
	}

	sliceType := reflect.SliceOf(t)
	sliceValue := reflect.MakeSlice(sliceType, 0, 0)
	realSlice := sliceValue.Interface()
	err := json.Unmarshal([]byte(data), &realSlice)
	if err != nil {
		fmt.Println(err)
	} else {
		rValue := reflect.ValueOf(realSlice)
		n := rValue.Len()
		for i := 0; i < n; i++ {
			value := rValue.Index(i)
			doMap := value.Interface().(map[string]interface{})
			id := doMap["id"]

			v := reflect.New(t)
			doMode := v.Interface()
			err := mapstructure.Decode(doMap, v.Interface())
			if err != nil {
				fmt.Println(err.Error())
			}
			if id != nil {
				idField := reflect.ValueOf(doMode).Elem().FieldByName("Id")
				fmt.Println(idField.CanSet())
				id := uint(id.(float64))
				idField.Set(reflect.ValueOf(id))
			}
			GetDb().Model(target).Create(doMode)

		}
	}

}

func InitRelationSystemResource() {
	for _, sql := range baseRelationSql {
		GetDb().Exec(sql)
	}
}

const baseUser = `
[{
	"id": 1,
	"uid": 1,
	"username": "admin",
	"name": "admin",
	"password": "123456",
	"mobile": "15793155366",
	"email": "425154211@gmail.com",
	"idCard": "912322199303056214",
	"state":1
}]
`

const baseMenu string = `
[{
	"id": 1,
	"name": "首页",
	"intro": "首页信息",
	"route": "/home",
	"icon": "HomeFilled"
}, {
	"id": 2,
	"name": "用户管理",
	"intro": "该菜单用于管理用户信息",
	"route": "/user",
	"icon": "Avatar"
}, {
	"id": 3,
	"name": "菜单管理",
	"intro": "该菜单用于管理菜单",
	"route": "/menu",
	"icon": "Menu"
}, {
	"id": 4,
	"name": "资源管理",
	"intro": "该菜单用于系统资源管理",
	"route": "/resource",
	"icon": "Fold"
}, {
	"id": 5,
	"name": "系统设置",
	"intro": "系统设置",
	"route": "/setting",
	"icon": "Setting"
}, {
	"id": 6,
	"parentId": 2,
	"name": "用户帐号",
	"intro": "该子菜单用于管理用户帐号的增删改查",
	"route": "/user/account",
	"icon": "User"
}, {
	"id": 7,
	"parentId": 2,
	"name": "用户群组",
	"intro": "该子菜单用于管理用户组的增删改查",
	"route": "/user/group",
	"icon": "Collection"
}, {
	"id": 8,
	"parentId": 2,
	"name": "用户角色",
	"intro": "该子菜单用于管理用户角色的增删改查",
	"route": "/user/role",
	"icon": "View"
}, {
	"id": 9,
	"parentId": 2,
	"name": "用户权限",
	"intro": "该子菜单用于管理用户权限的增删改查",
	"route": "/user/permission",
	"icon": "Operation"
}, {
	"id": 10,
	"parentId": 4,
	"name": "资源清单",
	"intro": "该子菜单用于管理用户权限的增删改查",
	"route": "/resource/list",
	"icon": "List"
}, {
	"id": 11,
	"parentId": 4,
	"name": "资源群组",
	"intro": "该子菜单用于管理资源组的增删改查",
	"route": "/resource/group",
	"icon": "Histogram"
}]
`

const basePermission = `[
{
	"id": 1,
	"name": "查询",
	"code": 1,
	"intro": "查询权限",
	"state":1
},{
	"id": 2,
	"name": "新增",
	"code": 2,
	"intro": "新增权限",
	"state":1
}, {
	"id": 3,
	"name": "修改",
	"code": 4,
	"intro": "修改权限",
	"state":1
}, {
	"id": 4,
	"name": "删除",
	"code": 8 ,
	"intro": "删除权限",
	"state":1
}]
`

const baseUserGroup = `
[{
	"id": 1,
	"parentId":0,
	"name": "超级管理员用户组",
	"intro": "该组只有超级管理员",
	"state":1
}]
`
const baseRole = `
[{
	"id": 1,
	"parentId": 0,
	"name": "超级管理员",
	"intro": "超级管理员",
	"state":1
}]
`
const baseResource = `
[{
  "name": "创建用户组",
  "path": "/user/group/create",
  "method": "POST",
	"state":1
}, {
  "name": "删除用户组",
  "path": "/user/group/delete",
  "method": "POST",
	"state":1
}, {
  "name": "注册用户",
  "path": "/user/register",
  "method": "POST",
	"state":1
}, {
  "name": "修改密码",
  "path": "/user/password/change",
  "method": "POST",
	"state":1
}, {
  "name": "用户登录",
  "path": "/user/login",
  "method": "POST",
	"state":1
}, {
  "name": "用户更新",
  "path": "/user/update",
  "method": "POST",
	"state":1
}, {
  "name": "创建资源组",
  "path": "/resource/group/create",
  "method": "POST",
	"state":1
}, {
  "name": "删除资源组",
  "path": "/resource/group/delete",
  "method": "POST",
	"state":1
}, {
  "name": "创建资源",
  "path": "/resource/create",
  "method": "POST",
	"state":1
}, {
  "name": "删除资源",
  "path": "/resource/delete",
  "method": "POST",
	"state":1
}, {
  "name": "创建角色",
  "path": "/role/create",
  "method": "POST",
	"state":1
}, {
  "name": "删除角色",
  "path": "/role/delete",
  "method": "POST",
	"state":1
}, {
  "name": "创建菜单",
  "path": "/menu/create",
  "method": "POST",
	"state":1
}, {
  "name": "删除菜单",
  "path": "/menu/delete",
  "method": "POST",
	"state":1
}, {
  "name": "创建权限",
  "path": "/permission/create",
  "method": "POST",
	"state":1
}, {
  "name": "删除权限",
  "path": "/permission/delete",
  "method": "POST",
	"state":1
}, {
  "name": "删除用户",
  "path": "/user/delete",
  "method": "DELETE",
	"state":1
}, {
  "name": "查询资源",
  "path": "/resource/list",
  "method": "GET",
	"state":1
}, {
  "name": "查询资源组",
  "path": "/resource/group/list",
  "method": "GET",
	"state":1
}, {
  "name": "查询查询角色",
  "path": "/role/list",
  "method": "GET",
	"state":1
}, {
  "name": "查询用户",
  "path": "/user/list",
  "method": "GET",
	"state":1
}, {
  "name": "查询用户组",
  "path": "/user/group/list",
  "method": "GET",
	"state":1
}, {
  "name": "查询菜单",
  "path": "/menu/list",
  "method": "GET",
	"state":1
}, {
  "name": "查询权限",
  "path": "/permission/list",
  "method": "GET",
	"state":1
}]
`

const baseResourceGroup = `
[{
	"id": 1,
	"name": "所有资源",
	"state":1
}]
`

// 超级管理员各个表关系
var baseRelationSql = [...]string{
	"insert into group_user (uid,group_id) select user.id  ,user_group.id  from user , user_group where user.id = 1 and user_group.id=1;",
	"insert into user_role (uid,role_id) select user.id  ,role.id  from user , role  where user.id = 1  and role.id = 1;",
	"insert into user_group_role (user_group_id,role_id )select user_group.id ,role.id from user_group,role where user_group.id = 1 and role.id = 1;",
	"insert into role_menu(menu_id,role_id) select menu.id,role.id from menu,role where role.id = 1 ;",
	"insert into role_permission(role_id,permission_id) select role.id,permission.id from role,permission where role.id = 1",
	"insert into role_resource(role_id,resource_id) select role.id ,resource.id from role,resource where role.id = 1;",
	"insert into resource_group_role (resource_group_id,role_id ) select  resource_group.id,role.id from resource_group,role where role.id = 1 and resource_group.id=1;",
	"insert into group_resource(resource_id,resource_group_id) select resource.id,resource_group.id from resource,resource_group where resource_group.id=1;",
}
